using System;
using System.Collections.Generic;
using System.Linq;
using ServiceMReviewer.Model;
using ServiceMReviewer.Helper;

namespace ServiceMReviewer.Repositories
{
	public class EstablishmentRepository
	{
		public List<Establishment> SelectAll()
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
                return ctx.Establishments.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Establishment>();
		}

		public Establishment SelectByID(string id)
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				return ctx.Establishments.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public bool Insert(Establishment obj)
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				ctx.Establishments.InsertOnSubmit(obj);
                ctx.SubmitChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Delete(string id)
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				var delitem = ctx.Establishments.FirstOrDefault(item => item.ID == id );
				ctx.Establishments.DeleteOnSubmit(delitem);
                ctx.SubmitChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Save()
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				ctx.SubmitChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public List<Establishment> RetrieveByID(string id)
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				return (from item in ctx.Establishments where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Establishment>();
		}

		public List<Establishment> SelectByIDEstablishmentType(int idestablishmenttype)
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				var lstItem = (from item in ctx.Establishments where item.IDEstablishmentType == idestablishmenttype select item).ToList();
				return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Establishment>();
		}

		public List<Establishment> SelectByIDFoodType(int idfoodtype)
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				var lstItem = (from item in ctx.Establishments where item.IDFoodType == idfoodtype select item).ToList();
				return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Establishment>();
		}

	}
}